using UnityEngine;
using System.Collections;
//using System;

public class bossteleport : BossCanTakeDamage
{
	Texture2D[] myTextures;
	public Texture2D boss1;
		public Texture2D boss3;
        public float speed = 25f;

	public Texture2D boss2;
	
/*
		 var textures : Texture2D[];
var delay = 1.0;
private var textureCounter = 0;
*/
	
	public float targetTime = 1.0f;
	float targetTimeRef;
	private float minY ;
	private float maxY;
	private float minX;
	private float maxX;
	private float minZ;
	private float maxZ;
	private Vector3 bossSize;
	private GameObject RocketRef;
	public GameObject Shockwave;
	public GameObject enemyRocket;
    private GameObject player;
    public int numberOfRockets = 20;
    public float rocketRadius = .5f;
    public float rocketSpeed = 50f;
    private bool move = false;
	// Use this for initialization
    float y = 0;
    public GUIStyle hodorStyle;
	   private float pointx,pointy;

    private float angle,angle1,angle2;
	
	private Vector3 projposition;
	void Start ()
	{
        FullHeal();
        player = GameObject.FindGameObjectWithTag("Player");
		myTextures = new Texture2D[3];
		myTextures[0] = boss1;
		myTextures[1] = boss2;
		
		myTextures[2] = boss3;
		
		// InvokeRepeating("CycleTextures", delay, delay);
		//mesh size for y coming out as 0 not sure why just using x look into in the future
		MeshFilter viewedModelFilter = (MeshFilter)gameObject.GetComponent ("MeshFilter");
		bossSize = viewedModelFilter.mesh.bounds.size * gameObject.transform.localScale.x;
		minX = WithinBounds.Bounds.minX + (bossSize.x / 2);
		maxX = WithinBounds.Bounds.maxX - (bossSize.x / 2);
		minY = WithinBounds.Bounds.minY / 2 + (bossSize.x) + .2f; //added factor for buildings;
		maxY = WithinBounds.Bounds.maxY - (bossSize.x / 2);
		minZ = 0; 
		maxZ = 0;		
		targetTimeRef = targetTime;
        StartCoroutine(Teleport(3f));
		
		//Debug.LogError(minX+" "+maxX+" "+minY+" "+maxY+" "+minZ+" "+maxZ);
	}
	
	// Update is called once per frame
	void Update ()
	{
        if (move)
        {
            transform.position = Vector3.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime);
        }
            //Debug.Log(targetTime);
        //targetTime -= Time.deltaTime;
        //transform.renderer.material.mainTexture = boss3;
        //if (targetTime <= 0.0f) {
        //    timerEnded ();
        //    targetTime = targetTimeRef;
        //}
	
	}
	/*
	void CycleTextures () {
    textureCounter = ++textureCounter % textures.length;
    renderer.material.mainTexture = textures[textureCounter];
	}
	*/
	void timerEnded ()
	{

        print(targetTime);
			 
		transform.position = new Vector3 (Random.Range (minX, maxX), Random.Range (minY, maxY), Random.Range (minZ, maxZ));
            RocketBurst();
		//for(int i, i <= explosionbullets.length, i _
		//Instantiate(gameObject, Vector3(Random.Range(minY,maxY), Random.Range(minZ,maxZ), Random.Range(minX,maxX)), Quaternion.identity)
		
        //int pickAttack = Random.Range(0,2);
        //if(pickAttack == 0)
        //ExplosionDamage(transform.position, 10);
        //if(pickAttack == 1)
        //RocketBurst();

        //testRockets();
	}

    public void OnGUI()
    {
        GUI.Box(new Rect(Screen.width / 2 - 150, Screen.height / 2 + 380, 350, 60), "BOSS 2: " + GetComponent<CanTakeDamage>().CurrHealth.ToString(), hodorStyle);
    }

//	void ExplosionDamage (Vector3 center, float radius)
//	{
//
//		GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
//		sphere.transform.position = transform.position;
//		sphere.transform.localScale = new Vector3(radius, radius, radius);
//				EditorApplication.isPaused = true;
////		sphere.SetActive(false);
//		Shockwave.transform.position = transform.position;
//		Shockwave.gameObject.SetActive(true);
//
//		//Instantiate(gameObject, transform.position, Quaternion.identity)
//		Collider[] hitColliders = Physics.OverlapSphere (center, radius);
//		foreach(Collider col in hitColliders)
//		{
//			if(col.gameObject.tag == "Player")
//			{
//				col.gameObject.GetComponent<CanTakeDamage>().TakeDamage(10);
//			}
//		}
//		  transform.renderer.material.mainTexture = boss1;
//	}
	void ShockwaveAttack()
	{
		Instantiate (Shockwave, transform.position, Quaternion.Euler(270,0,0));
	}
 	
	void RocketBurst ()
    {

        //angle2 = Random.Range(0f, 361f);
		for (var i = angle2; i < numberOfRockets; i++) {
			angle = i * Mathf.PI * 2 / numberOfRockets;
			Vector3 pos = new Vector3 (Mathf.Cos (angle), Mathf.Sin (angle), 0) * rocketRadius;
            RocketRef = Instantiate(enemyRocket, transform.position + pos, Quaternion.Euler(new Vector3(Mathf.Rad2Deg * -angle, 90, 270))) as GameObject; //they all need to be facing in there own forward not a global forward
            RocketRef.rigidbody.mass = 1f;
			RocketRef.rigidbody.AddForce(RocketRef.transform.forward * rocketSpeed);
			RocketRef.rigidbody.mass = .000001f;
		}


		Debug.Log("rockets");
	}
	IEnumerator Teleport(float waitTime)
    {
        //yield return new WaitForSeconds(2f);
        //transform.position = new Vector3(Random.Range(minX, maxX), Random.Range(minY, maxY), Random.Range(minZ, maxZ));
        move = true;
		
		yield return new WaitForSeconds(3f);
        // rigidbody.AddForce(player.transform.position * 40f);
        move = false;
        int pickAttack = Random.Range(0, 2);
        if (pickAttack == 0)
        {
           move = false;
            transform.renderer.material.mainTexture = boss1;
            yield return new WaitForSeconds(.1f);
            ShockwaveAttack();
			            yield return new WaitForSeconds(1f);

            move = true;
			
        }
        if (pickAttack == 1)
        {
            move = false;
            transform.renderer.material.mainTexture = boss2;
            yield return new WaitForSeconds(.1f);
            RocketBurst();
			            yield return new WaitForSeconds(1f);
			  move = true;
          
        }

        yield return new WaitForSeconds(1f);
        transform.renderer.material.mainTexture = boss3;
        move = true;
        StartCoroutine(Teleport(3f));
    }
}
